﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace FileLockUtility
{
    public partial class MainForm : Form
    {
        FileStream fileStream;

        public MainForm()
        {
            InitializeComponent();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtFullFileName.Text = String.Empty;
        }

        private void btnLock_Click(object sender, EventArgs e)
        {
            if (File.Exists(txtFullFileName.Text))
            {
                fileStream = new FileStream(txtFullFileName.Text, FileMode.Open);
                fileStream.Lock(0, 0);
            }
            else
            {
                MessageBox.Show("The specified file does not exist.");
            }
        }

        private void btnUnlock_Click(object sender, EventArgs e)
        {
            if (fileStream != null)
            {
                fileStream.Unlock(0, 0);
                fileStream.Close();
                fileStream.Dispose();
            }
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            DialogResult result;
            result = openFileDialog.ShowDialog();

            if (result == DialogResult.OK)
            {
                txtFullFileName.Text = openFileDialog.FileName;
            }
        }

    }
}
